# coding=utf-8
import json
import pika


# 设置MQ环境
env = "pt_test"

if env == "localhost":
    mq_info = {
        "username": "guanfuchang",
        "password": "12345678",
        "host": "localhost",
        "virtual_host": "/vhost_qa"
    }
elif env == "pt_test":
    mq_info = {
        "username": "",
        "password": "",
        "host": "",
        "virtual_host": ""
    }


def get_connection():
    """
    获取MQ连接
    :return:
    """
    # 连接MQ
    user_pwd = pika.PlainCredentials(username=mq_info.get("username"), password=mq_info.get("password"))
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=mq_info.get("host"),
                                                                   virtual_host=mq_info.get("virtual_host"),
                                                                   credentials=user_pwd))
    return connection


# 获取MQ连接
connection = get_connection()

# 在连接上创建一个频道CHANNEL
channel = connection.channel()

# 创建模糊匹配类型的exchange。
channel.exchange_declare(exchange='rfchina.platform', exchange_type='topic', durable=True)

# 发送消息
data = {
    'status': 4,
    'audit_time': '2021-07-12 11:16:20',
    'tax_type': 0,
    'sids': 'RFJC8202107121115560000661'
}
channel.basic_publish(exchange='rfchina.platform',  # 指明用于发布消息的交换机
                      routing_key='settlec.manager.audit.success.notify',  # 路由键，绑定关键字，将队列变成[warn]日志的专属队列
                      body=json.dumps(data,ensure_ascii=False)  # 生产者要发送的消息
                      )
print("[生产者] send %s'" %json.dumps(data,ensure_ascii=False))

connection.close()  # 当生产者发送完消息后，可选择关闭连接
